# -*- coding: utf-8 -*- 
# @Time : 2021-02-06 10:05 
# @Author : derek.zhang 
# @File : verifycode.py 
# @Software: PyCharm
from django.http import HttpResponse


def verify_code(request):

    from PIL import  Image, ImageDraw, ImageFont

    import random
    # 定义 画面背景色 宽，高
    bgcolor = (random.randrange(40, 200), random.randrange(40, 200), 255)
    width = 200
    height = 40
    # 创建画面对象
    im = Image.new('RGB', (width, height), bgcolor)
    # 创建画笔
    draw = ImageDraw.Draw(im)

    # 调用画笔的point() 函数绘制噪点
    for i in range(0,100):
        xy = (random.randrange(0, width), random.randrange(0, height))
        fill = (random.randrange(0, 255), 255, random.randrange(0, 255))
        draw.point(xy, fill=fill)

    str1 = 'ABCD123EFGHIJK456LMNOPQRS789TUVWXYZ0'
    rand_str = ''

    for i in range(4):
        rand_str += str1[random.randrange(0, len(str1))]
    # rand_str += ''.join(random.sample(str1, 4))

    # 构造字体对象
    font = ImageFont.load_default().font
    # font = ImageFont.truetype('C:\Windows\Fonts\Arial.ttf', 23)
    # 构造字体颜色
    fontcolor = (255, random.randrange(0, 255), random.randrange(0, 255))
    # 绘制4个字
    draw.text((20, 10), rand_str[0], font=font, fill=fontcolor)
    draw.text((70, 10), rand_str[1], font=font, fill=fontcolor)
    draw.text((120, 10), rand_str[2], font=font, fill=fontcolor)
    draw.text((170, 10), rand_str[3], font=font, fill=fontcolor)
    # 释放画笔
    del draw

    request.session['verify_code'] = rand_str

    import io
    buf = io.BytesIO()
    im.save(buf, 'png')
    return HttpResponse(buf.getvalue(), 'image/png')